// pages/record/record.ts
import { Request } from '../../utils/util'
import Notify from '@vant/weapp/notify/notify';
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        navBarData: { // 导航栏的数据
            title: '借用记录',
            scan: true,
            navigateBack: false
        },
        deviceList: [],
        deviceListSelected: [], // 选择的设备类别列表
        notReturnNum: 0,
        typeList: [], // 所有设备类型,
        active: 0,
        pickerIndex: 0,
        array: ['隐藏已归还', '全部显示'],
        showOverlay: false,
        deviceSelect: {}, // 选择的设备(想要借用)
        loading:true
    },
    scan(e){
        let res = e.detail
        console.log('扫码结果：',res)
    },
    bindPickerChange(e) {
        let deviceListSelectedTmp = []
        if (e.detail.value == 0) { //隐藏已归还
            deviceListSelectedTmp = this.data.deviceList.filter(obj => obj.devicetype === this.data.typeList[this.data.active] && obj.isreturn === 'false')
        } else {
            deviceListSelectedTmp = this.data.deviceList.filter(obj => obj.devicetype === this.data.typeList[this.data.active])
        }
        // 对deviceListSelectedTmp进行排序，按照isreturn状态进行排序，true排在后面
        deviceListSelectedTmp.sort((a, b) => {
            if (a.isreturn === 'true') {
                return 1
            }
            if (b.isreturn === 'true') {
                return -1
            }
        })
        this.setData({
            pickerIndex: e.detail.value,
            deviceListSelected: deviceListSelectedTmp,
            notReturnNum: deviceListSelectedTmp.filter(obj => obj.isreturn === 'false').length
        })

    },

    onChange(event) {
        let deviceListSelectedTmp = []
        if (this.data.pickerIndex == 0) {
            deviceListSelectedTmp = this.data.deviceList.filter(obj => obj.devicetype === this.data.typeList[event.detail.name] && obj.isreturn === 'false')
        } else { //全部显示
            deviceListSelectedTmp = this.data.deviceList.filter(obj => obj.devicetype === this.data.typeList[event.detail.name])
        }
        // 对deviceListSelectedTmp进行排序，按照isreturn状态进行排序，true排在后面
        deviceListSelectedTmp.sort((a, b) => {
            if (a.isreturn === 'true') {
                return 1
            }
            if (b.isreturn === 'true') {
                return -1
            }
        })
        this.setData({
            deviceListSelected: deviceListSelectedTmp,
            active: event.detail.name,
            notReturnNum: deviceListSelectedTmp.filter(obj => obj.isreturn === 'false').length
        })

    },
    borrow(e) {
        let device = e.detail
        this.setData({
            deviceSelect: device
        })
        this.onClickShow()
    },

    onClickHide() {
        this.setData({
            showOverlay: false
        })
    },

    onClickShow() {
        this.setData({
            showOverlay: true
        })
    },

    returnAll() {
        if(this.data.notReturnNum == 0){
            return
        }
        let that = this
        wx.showModal({
            title: '提示',
            content: '确认全部归还',
            success(res) {
                if (res.confirm) {
                    // 遍历数组deviceListSelected，如果isreturn属性为false，则调用deviceReturn方法
                    that.data.deviceListSelected.forEach(obj => {
                        if (obj.isreturn === 'false') {
                            that.funDeviceReturn(obj.id)
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },

    deviceReturn() {
        console.log('归还设备')
        let that = this
        if (this.data.deviceSelect.isreturn === 'true') {
            console.log('已归还')
        } else {
            wx.showModal({
                title: '提示',
                content: '确认归还',
                success(res) {
                    if (res.confirm) {
                        that.funDeviceReturn(that.data.deviceSelect.id)
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        }
    },
    funDeviceReturn(id) {
        Request({
            url: '/devicereturn',
            method: 'POST',
            data: {
                data: {
                    id: id
                }
            }
        }).then(res => {
            if (res.success) {
                wx.showToast({
                    title: '归还成功'
                })
            } else {
                wx.showToast({
                    title: '归还失败',
                    icon: 'error'
                })
            }
            this.onShow()
        }).catch(error => {
            console.log(error);
        });
    },
    borrowAgain(e) {
        console.log('续借',this.data.deviceSelect.id,`pages/renew/renew?id=${this.data.deviceSelect.id}`)
        wx.navigateTo({
            url: `/pages/renew/renew?id=${this.data.deviceSelect.id}`
        })
    },
    scan(){
        let that = this
        wx.scanCode({
            onlyFromCamera: true,// 只允许从相机扫码
            scanType: ['qrCode'],
            success(res) {
                // 扫码成功后  在此处理接下来的逻辑
                that.triggerEvent('scan',res.result)
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady(){
        this.setData({loading:false})
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        Request({
            url: '/getborrow',
        }).then(res => {
            // 将res排序，isreturn为false的排在前面
            console.log(res)
            res.sort((a, b) => {
                if (a.isreturn === 'true') {
                    return 1
                }
                if (b.isreturn === 'true') {
                    return -1
                }
            })

            let typeListTmp = [...new Set(res.map(obj => obj.devicetype))]
            let deviceListSelectedTmp = []
            if (this.data.pickerIndex == 0) {//隐藏已归还
                deviceListSelectedTmp = res.filter(obj => obj.devicetype === typeListTmp[this.data.active] && obj.isreturn === 'false')
            } else {
                deviceListSelectedTmp = res.filter(obj => obj.devicetype === typeListTmp[this.data.active])
            }
            // 对deviceListSelectedTmp进行排序，按照isreturn状态进行排序，true排在后面
            deviceListSelectedTmp.sort((a, b) => {
                if (a.isreturn === 'true') {
                    return 1
                }
                if (b.isreturn === 'true') {
                    return -1
                }
            })
            this.setData({
                deviceList: res,
                typeList: typeListTmp,
                deviceListSelected: deviceListSelectedTmp,
                notReturnNum: deviceListSelectedTmp.filter(obj => obj.isreturn === 'false').length
            })

        }).catch(error => {
            console.log(error);
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        setTimeout(function () {
            wx.reLaunch({
                url: '/pages/record/record'
            })
        }, 1000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})
